package com.dengdai.applibrary.utils;

import android.util.Log;

import java.util.Locale;

/**
 * @说明 日志输出
 * @作者 LY
 * @文件 LogUtils.java
 * @时间 2015年6月11日 下午9:36:19
 * @版权 Copyright(c) 2014 LY-版权所有
 */
public class LogUtils {

    public static void v(String tag,String mess) {
        if (true) {
            Log.v(tag+getTag(),getTags()+" "+ buildMessage(mess));
        }
    }

    public static void d(String tag,String mess) {
        if (true) {
            Log.d(tag+getTag(),getTags()+" "+ buildMessage(mess));
        }
    }

    public static void i(String tag,String mess) {
        if (true) {
            Log.i(tag+getTag(),getTags()+" "+ buildMessage(mess));
        }
    }

    public static void w(String tag,String mess) {
        if (true) {
            Log.w(tag+getTag(),getTags()+" "+ buildMessage(mess));
        }
    }

    public static void e(String tag,String mess) {
        if (true) {
            Log.e(tag+getTag(),getTags()+" "+ buildMessage(mess));
        }
    }

    public static void v(String mess) {
        if (true) {
            v("",mess);
        }
    }

    public static void d(String mess) {
        if (true) {
            d("",mess);
        }
    }

    public static void i(String mess) {
        if (true) {
            i("",mess);
        }
    }

    public static void w(String mess) {
        if (true) {
            w("",mess);
        }
    }

    public static void e(String mess) {
        if (true) {
            e("",mess);
        }
    }

    private static String buildMessage(String msg) {
        StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
        String caller = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtils.class)) {
                caller = trace[i].getMethodName();
                break;
            }
        }
        if (null == msg) {
            msg = "";
        }
        return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller, msg);
    }


    /**
     * 获取日志的标签 格式：类名_方法名_行号 （需要权限：android.permission.GET_TASKS）
     *
     * @return tag
     * @see [类、类#方法、类#成员]
     */
    /**
     * 异常栈位移
     */
    private static final int EXCEPTION_STACK_INDEX = 2;
    private static String getTags()
    {
        try
        {
            Exception exception = new LogException();
            if (exception.getStackTrace() == null || exception.getStackTrace().length <= EXCEPTION_STACK_INDEX)
            {
                return "***";
            }
            StackTraceElement element = exception.getStackTrace()[EXCEPTION_STACK_INDEX];

            String className = element.getClassName();

            int index = className.lastIndexOf(".");
            if (index > 0)
            {
                className = className.substring(index + 1);
            }

            return className + "_" + element.getMethodName() + "_" + element.getLineNumber();

        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return "***";
        }
    }

    private static String getTag() {
        StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
        String callingClass = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtils.class)) {
                callingClass = trace[i].getClassName();
                callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
                break;
            }
        }
        return " Hanjh";
        //        return callingClass;
    }

    /**
     * 取日志标签用的的异常类，只是用于取得日志标签
     */
    private static class LogException extends Exception
    {
        /**
         * 注释内容
         */
        private static final long serialVersionUID = 1L;
    }

}